package com.ruoyi.app.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.app.pojo.dto.UserCodeLoginDto;
import com.ruoyi.app.pojo.dto.UserGetSmsCodeDto;
import com.ruoyi.app.pojo.vo.UserGetCaptchaImageVo;
import com.ruoyi.app.pojo.vo.UserGetInfoVo;
import com.ruoyi.app.pojo.vo.UserGetSmsCodeVo;
import com.ruoyi.app.service.impl.AppLoginService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginAppBody;
import com.ruoyi.framework.security.annotation.Anonymous;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ClassName：AppLoginController
 * Package：com.ruoyi.applet.controller
 * Description：
 *
 * @Author：fpy
 * @Create：2025/5/12 - 14:36
 * @Version：v1.0
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private AppLoginService appLoginService;

    /**
     * 微信小程序一键登录
     *
     * @param: loginAppBody
     * @return: com.ruoyi.common.core.domain.AjaxResult
     * @author: fpy
     * @date: 2025/5/12 15:52
    **/
    @Anonymous
    @PostMapping("/minLogin")
    public AjaxResult minLogin(@RequestBody LoginAppBody loginAppBody) {
        if (StrUtil.isBlank(loginAppBody.getCode())) {
            return AjaxResult.error("参数不完整");
        }

        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appLoginService.minLogin(loginAppBody.getCode());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 生成图形验证码
     *
     * @param: response
     * @param: phone
     * @return: com.ruoyi.common.core.domain.AjaxResult
     * @author: fpy
     * @date: 2025/5/13 10:22
    **/
    @Anonymous
    @PostMapping("/getCaptchaImage")
    public AjaxResult<UserGetCaptchaImageVo> getCaptchaImage() {
        return appLoginService.getCaptchaImage();
    }

    /**
     * 获取手机验证码
     *
     * @param: dto
     * @return: com.ruoyi.common.core.domain.AjaxResult
     * @author: fpy
     * @date: 2025/5/13 11:06
    **/
    @Anonymous
    @PostMapping("/getSmsCode")
    public AjaxResult<UserGetSmsCodeVo> getSmsCode(@RequestBody @Validated UserGetSmsCodeDto dto) {
        return appLoginService.getSmsCode(dto);
    }

    /**
     * 短信验证码登录
     *
     * @param: loginAppBody
     * @return: com.ruoyi.common.core.domain.AjaxResult
     * @author: fpy
     * @date: 2025/5/12 17:32
    **/
    @Anonymous
    @PostMapping("/codeLogin")
    public AjaxResult codeLogin(@RequestBody @Validated UserCodeLoginDto dto) {
        return appLoginService.codeLogin(dto);
    }

    /**
     * 获取用户信息
     *
     * @param:
     * @return: com.ruoyi.common.core.domain.AjaxResult
     * @author: fpy
     * @date: 2025/5/12 15:30
    **/
    @PostMapping("/getInfo")
    public AjaxResult<UserGetInfoVo> getInfo() {
        return appLoginService.getInfo();
    }

    /**
     * 账号密码登录
     *
     * @param: loginAppBody
     * @return: com.ruoyi.common.core.domain.AjaxResult
     * @author: fpy
     * @date: 2025/5/12 15:30
     **/
//    @Anonymous
//    @PostMapping("/login")
//    public AjaxResult login(@RequestBody LoginAppBody loginAppBody) {
//        AjaxResult ajax = AjaxResult.success();
//        // 生成令牌
//        String token = appLoginService.login(loginAppBody.getUsername(), loginAppBody.getPassword());
//        ajax.put(Constants.TOKEN, token);
//        return ajax;
//    }

}
